<template>
  <el-container class="home-container">
    <!--头部-->
    <el-header>
      <div class="log-box">
        <img src="../assets/title.png" alt=""/>
        <span>电商后台管理系统</span>
      </div>
      <el-button @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <!--侧边栏-->
      <el-aside :width="isCollapse === true ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <!--菜单组件-->
        <el-menu
          :default-active="this.$route.path"
          class="el-menu"
          background-color="#333744"
          text-color="#fff"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          router
        >
          <el-submenu v-for="item in menusList" :key="item.id" :index="item.id+''">
            <!--一级菜单的模板区域-->
            <template slot="title">
              <!--图标-->
              <i :class="iconsObj[item.id]"></i>
              <!--文本-->
              <span>{{item.authName}}</span>
            </template>
            <!--二级菜单-->
            <el-menu-item v-for="subItem in item.children" :key="subItem.id" :index="'/'+subItem.path">
              <template slot="title">
                <!--图标-->
                <i class="el-icon-menu"></i>
                <!--文本-->
                <span>{{subItem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!--主界面-->
      <el-main>
        <!--路由占位符-->
        <router-view></router-view>

      </el-main>
    </el-container>
  </el-container>
</template>

<script>

  export default {
    data () {
      return {
        //左侧菜单栏数据
        menusList: [],
        iconsObj: {
          '125': 'iconfont icon-yonghuguanli',
          '103': 'iconfont icon-quanxianguanli',
          '101': 'iconfont icon-shangpinguanli',
          '102': 'iconfont icon-tubiao_dingdanguanli',
          '145': 'iconfont icon-shujutongji'
        },
        //是否折叠菜单
        isCollapse: false
      }
    },
    created () {
      this.getMenusList()
      console.log("55")
      console.log(this.$route)
    },
    methods: {
      logout () {
        window.sessionStorage.removeItem('token')
        this.$router.push('/login')
      },
      //获取菜单数据列表
      async getMenusList () {
        let { data: res } = await this.$http.get('/menus')
        if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
        console.log(res)
        this.menusList = res.data
      },
      //点击按钮切换菜单的折叠
      toggleCollapse () {
        this.isCollapse = !this.isCollapse
      }
    }

  }
</script>

<style lang="stylus" scoped>//scoped代表样式只作用于当前组件
.home-container {
  height 100%
}

.el-header {
  background-color #373D41
  height 60px

  display flex
  justify-content space-between
  align-items center
  padding-left 0
  color #FFFFFF
  font-size 22px

  > div {
    display flex
    align-items center

    img {
      width 50px
      height 50px
      margin-right 10px
    }

  }
}

.el-aside {
  background-color #333744

  .el-menu {
    border-right none
    text-align left
  }

  .toggle-button {
    color: #fff;
    font-size: 12px;
    text-align: center;
    line-height: 25px;
    background-color: #4a5064;
    user-select: none;
    cursor: pointer;
    letter-spacing: .1em;
  }
}

.el-main {
  background-color #eaedf1
}

.iconfont {
  margin-right 10px
}
</style>
